<template>
  <BasicModal
    v-bind="$attrs"
    :title="title"
    @register="registerInnerModal"
    @ok="handleSubmit"
    @cancel="resetForm"
  >
    <BasicForm @register="registerForm" />
    <a-button @click="handleOpenModal"> 选择商品 </a-button>
  </BasicModal>
  <ProductInfoModal
    @register="registerTable"
    @handle-submit="handleModalSubmit"
    @handle-close="handleModalClose"
    v-if="modalVisible"
  />
</template>

<script setup lang="ts">
  import { BasicModal, useModalInner, useModal } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { computed, ref, unref } from 'vue';
  import {
    otaLinkProductInfoInfo,
    otaLinkProductInfoAdd,
    otaLinkProductInfoUpdate,
  } from '@/api/ota/otaactivity';
  import { modalSchemas } from './otaLinkProductInfo.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import ProductInfoModal from './ProductInfoModal.vue';

  const { createMessage } = useMessage();
  const [registerTable, { openModal }] = useModal();

  defineOptions({ name: 'OtaLinkProductInfoModal' });

  const modalVisible = ref(false);
  const emit = defineEmits(['register', 'reload']);
  const isUpdate = ref<boolean>(false);
  const title = computed<string>(() => {
    return isUpdate.value ? '编辑OTA商品关联信息' : '新增OTA商品关联信息';
  });

  const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
    async (data: { record?: Recordable; update: boolean }) => {
      modalLoading(true);
      const { record, update } = data;
      isUpdate.value = update;
      if (update && record) {
        const ret = await otaLinkProductInfoInfo(record.id);
        await setFieldsValue(ret);
      }
      modalVisible.value = true;
      modalLoading(false);
    },
  );

  const [registerForm, { setFieldsValue, resetForm, validate, getFieldsValue }] = useForm({
    labelWidth: 100,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
    schemas: modalSchemas,
  });

  //选中商品后
  function handleModalSubmit(productListValue) {
    if (productListValue.length > 1) {
      createMessage.error('请勿选择多种商品！');
      return;
    }
    setFieldsValue({
      linkProductId: productListValue[0].id,
      productName: productListValue[0].productName,
      linkType: productListValue[0].productType,
      linkProductType: productListValue[0].productClassKey,
    });
    modalVisible.value = false;
  }

  //关闭
  function handleModalClose() {
    modalVisible.value = false;
  }

  //打开商品列表
  function handleOpenModal() {
    const data = getFieldsValue();
    if (data.shopId == undefined) {
      createMessage.error('请选择选择店铺再选择商品信息！');
      return;
    }
    modalVisible.value = true;
    openModal(true, { shopId: data.shopId });
  }

  async function handleSubmit() {
    try {
      modalLoading(true);
      const data = await validate();
      if (unref(isUpdate)) {
        await otaLinkProductInfoUpdate(data);
      } else {
        await otaLinkProductInfoAdd(data);
      }
      emit('reload');
      closeModal();
      await resetForm();
    } catch (e) {
    } finally {
      modalLoading(false);
    }
  }
</script>

<style scoped></style>
